import { MotaInterface } from "@/mota";
import { ImageName } from "@/mota/data/ImageLoader";
import { Keyboard } from "@/mota/data/KeyDataUtils";
import PackUtils from "@/mota/utils/HeroUtils/PackUtils";
import EnemyMapUtils from "@/mota/utils/HeroUtils/props/EnemyMapUtils";
import YellowDoor from "@/mota/utils/HeroUtils/consumables/YellowDoor";
import JumpFloorUtils from "@/mota/utils/HeroUtils/props/JumpFloorUtils";

export type AttributeUnit = "点" | "枚" | "把" | "扇" | "本" | "柄";
export type AttributeInfo<T> = readonly [id: T, name: string, unit: AttributeUnit, image: ImageName];
const MAIL = <T>(data: readonly AttributeInfo<T>[]) => data;
export const heroAttributeList = MAIL(<const>[
    ["hp", "生命值", "点", "血量"],
    ["attack", "攻击力", "点", "神圣剑"],
    ["defence", "防御力", "点", "神圣盾"],
    ["gold", "金币", "枚", "幸运金币"],
    ["exp", "经验", "点", "经验卷轴"],
    ["keyYellow", "黄钥匙", "把", "黄钥匙"],
    ["keyRed", "红钥匙", "把", "红钥匙"],
    ["keyBlue", "蓝钥匙", "把", "蓝钥匙"],
    ["keyGreen", "绿钥匙", "把", "绿钥匙"],
    ["keyIron", "铁门钥匙", "把", "铁门钥匙"],
]);
export type AttributeType = typeof heroAttributeList[number][0];

export type HeroAttribute = { [attr in AttributeType]: number; };
export type HeroAttributeMap = { [attr in AttributeType]: AttributeInfo<attr>; };
export const heroAttributeMap = {} as HeroAttributeMap;
for (const info of heroAttributeList) {
    (<any>heroAttributeMap)[info[0]] = info;
}

export type GetItemEmit = readonly [AttributeType, number];
export type GetItemMap = { readonly [name in ImageName]?: GetItemEmit[]; };
export const getItemMap: GetItemMap = {
    "黄钥匙": [["keyYellow", 1]],
    "红钥匙": [["keyRed", 1]],
    "蓝钥匙": [["keyBlue", 1]],
    "绿钥匙": [["keyGreen", 1]],
    "铁门钥匙": [["keyIron", 1]],
    "普通钥匙盒": [["keyYellow", 1], ["keyRed", 1], ["keyBlue", 1]],
    "宝石钥匙盒": [["keyYellow", 3], ["keyRed", 3], ["keyBlue", 3]],
    "红宝石": [["attack", 3]],
    "蓝宝石": [["defence", 3]],
    "绿宝石": [], // 1% 暴击率
    "黄宝石": [], // 1% 闪避率
    "小血瓶": [["hp", 200]],
    "大血瓶": [["hp", 500]],
    "经验药水": [["exp", 300]],
    "敏捷药水": [],// 10% 持续1分钟
    "铁剑": [["attack", 10]],
    "银剑": [["attack", 30]],
    "骑士剑": [["attack", 50]],
    "圣剑": [["attack", 100]],
    "神圣剑": [["attack", 220]],
    "幸运金币": [["gold", 300]],
    "铁盾": [["defence", 10]],
    "银盾": [["defence", 30]],
    "骑士盾": [["defence", 50]],
    "圣盾": [["defence", 80]],
    "神圣盾": [["defence", 220]],
};

export const useItemMap: GetItemMap = {
    "黄门": [["keyYellow", 1]],
    "红门": [["keyRed", 1]],
    "蓝门": [["keyBlue", 1]],
    "铁门": [["keyIron", 1]],
};

export interface SaveItemHandle<T extends string> {
    pack: PackUtils;
    itemType: T;
    Use: () => Promise<boolean>;
    SetPack: (pack: PackUtils, itemType: T) => void
};
export type SaveItemInfo<T extends string> = readonly [remarks: string, keyboard: Keyboard, handle: SaveItemHandle<T> & MotaInterface, unit: AttributeUnit];
export type SaveItemMap<T extends string> = { readonly [name in ImageName]?: SaveItemInfo<T>; };

export type ConsumablesInterface = SaveItemHandle<"consumables">;
export type PropsInterface = SaveItemHandle<"props">;

export const saveConsumablesMap: SaveItemMap<"consumables"> = {
    "随意门": ["将面前可变成黄门的墙换为黄门", "g", new YellowDoor(), "扇"],
};

export const savePropsMap: SaveItemMap<"props"> = {
    "怪物图鉴": ["查看当前楼层的怪物信息", "e", new EnemyMapUtils(), "本"],
    "奇怪的权杖": ["可传送于各楼层", "f", new JumpFloorUtils(), "柄"],
};

export function ParseUnit(name: string, count: number, unit: AttributeUnit, always?: boolean) {
    if (Math.abs(count) <= 1 && !always) {
        return name;
    } else {
        return `${count}${unit}${name}`;
    }
};